package com.test.auth_db_auto.controller;

import com.test.auth_db_auto.support.BootSecurityProperties;
import com.test.auth_db_auto.support.properities.BootBaseLoginProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @ Author: .Mr
 * @ ClassName BaseMainController
 * @ Description TODO
 * @ date 2021/12/25 15:35
 * @ Version 1.0
 */
@Controller
public class BaseMainController {

    @Autowired
    private BootSecurityProperties properties;

    @RequestMapping("${boot.oauth.login-page:/auth/login}")
    public String loginPage(Model model, HttpServletRequest request){
        BootBaseLoginProperties base=properties.getBaseLogin();
        String type=request.getParameter("type");
        Object error=request.getAttribute("error");
        String errorInfo=error==null?null:error.toString();
        model.addAttribute("username",request.getParameter(base.getUsernameParameterName()));
        model.addAttribute("password",request.getParameter(base.getPasswordParameterName()));
        model.addAttribute("error", errorInfo);
        if(type!=null){
            if(errorInfo!=null&&errorInfo.contains("用户需要设置密码(唯一标识):")&&type.equalsIgnoreCase("sms")){
                String [] info=errorInfo.split(":");
                model.addAttribute("sole",info[1]);
                model.addAttribute("type","sms");
                model.addAttribute("mobile",request.getParameter(properties.getSmsLogin().getMobileParameterName()));
            }else if(type.equalsIgnoreCase("base")||
                    type.equalsIgnoreCase("sms")||
                    type.equalsIgnoreCase("social")){
                model.addAttribute("type",type);
            }
        }else{
            model.addAttribute("type","base");
        }

        model.addAttribute("sms", properties.getSmsLogin());
        model.addAttribute("base",properties.getBaseLogin());
        model.addAttribute("codePath",properties.getCodePath());
        return "base-login";
    }

}
